import AITalk from '../talkTemplate';
import { GetChangedNextStepMethod } from './defination';
const node = AITalk.getTalks('optionJudge');
/**
 * 获取下一步变更的数据
 * @param { Object } preNext 初始的下一步数据 {other: ''}
 * @param { Object } curNext 当前的下一步数据  {other: ''}
 * @param { Array } preOptionList 初始的选项列表  []
 * @param { Array } curOptionList 当前的选项列表  []
 */
export const getChangedNextStep = (preNext = {}, curNext = {}, preOptionList, curOptionList) => {
	let changedNextSteps = [];
	if (!preNext && !curNext && !preOptionList && !curOptionList) {
		return [];
	}

	if (preOptionList.length > curOptionList.length) {
		// 删除下一步节点
		preOptionList.map((item, index) => {
			if (curOptionList[index]) {
				if (item.nextStepOption !== curOptionList[index].nextStepOption) {
					changedNextSteps.push({
						text: `选项${index + 1}`,
						type: `option${index + 1}`,
						previous: item.nextStepOption,
						current: curOptionList[index].nextStepOption
					});
				}
			} else {
				changedNextSteps.splice(0, 0, {
					text: `选项${index + 1}`,
					type: `option${index + 1}`,
					previous: item.nextStepOption,
					current: null
				});
			}
		});
	} else {
		// 新增下一步节点
		curOptionList.map((item, index) => {
			if (preOptionList[index]) {
				if (item.nextStepOption !== preOptionList[index].nextStepOption) {
					changedNextSteps.push({
						text: `选项${index + 1}`,
						type: `option${index + 1}`,
						previous: preOptionList[index].nextStepOption,
						current: item.nextStepOption
					});
				}
			} else {
				changedNextSteps.push({
					text: `选项${index + 1}`,
					type: `option${index + 1}`,
					previous: null,
					current: item.nextStepOption
				});
			}
		});
	}

	if (preNext['other'] !== curNext['other']) {
		changedNextSteps.push({
			text: '其他选项',
			type: node.errorRemind.type,
			previous: preNext['other'],
			current: curNext['other']
		});
	}

	return changedNextSteps;
};
GetChangedNextStepMethod['optionJudge'] = getChangedNextStep;
